<?php
/**
 * LunchController class.
 * LunchController is the controller for business portal.
 * 
 * It used for all "lunch offer" product.
 * 
 * Yawii Business 版权所有 @ 2012.
 */
class LunchController extends Controller
{
	/**
	 * @return array action filters
	 */
	public function filters() {
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}
	
	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules() {
		return array(
			array('allow', // allow authenticated user to perform 'logout' and 'index' action
				'actions'=>array('index', 'menu', 'offer'),
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'actions'=>array('index', 'menu', 'offer'),
				'users'=>array('*'),
			),
		);
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
	    if ($error = Yii::app()->errorHandler->error) {
	    	if (Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('index', $error);
	    }
	}
	
	public function actionIndex()
	{
		$model = $this->_loadModel(Yii::app()->user->id);
		$model->setScenario('profile');
		
		//fetch menu and lunch offer
		$week_days = Yii::app()->request->getQuery('week', date('N'));	//get week day number: 1 (for Monday) - 7 (for Sunday)
		if ($week_days < 1 or $week_days > 7) $week_days = date('N');
		
		$connection = Yii::app()->db;
		//1. fetch menu
		$sql_menu = 'SELECT id, url_picture, content, title,
			UNIX_TIMESTAMP(insert_timestamp) AS uts_insert, UNIX_TIMESTAMP(update_timestamp) AS uts_update
			FROM tbl_merchant_lunch_menu
			WHERE id_merchant=:id_merchant AND week_days LIKE :week_days';
		$params_menu = array('id_merchant'=>(int)Yii::app()->user->id, 'week_days'=>"%{$week_days}%");
		$command = $connection->createCommand($sql_menu);
		$menu_rows = $command->queryAll(true, $params_menu);
		
		//2. fetch lunch special
		$sql_special = 'SELECT id, url_picture, content,
			show_price, price, value, 100-(price/value * 100) AS discount,
			show_hours, start_hour, end_hour, week_days,
			UNIX_TIMESTAMP(start_date) AS uts_start_date, UNIX_TIMESTAMP(end_date) AS uts_end_date
			FROM tbl_merchant_special
			WHERE id_merchant=:id_merchant AND week_days LIKE :week_days
			AND trash=0 AND status=1 AND start_date<NOW() AND end_date>NOW()';
		$command = $connection->createCommand($sql_special);
		$special_rows = $command->queryAll(true, array('id_merchant'=>(int)Yii::app()->user->id, 'week_days'=>"%{$week_days}%"));
		
		//3. fetch merchant tags
		$sql_tags = 'SELECT cuisine_style, cuisine_type, meal_served, payment_options, service_languages, 
			alcohol_license, dining_ambience, car_parking, additional_details
			FROM tbl_merchant_tags WHERE id_merchant=:id_merchant';
		$command = $connection->createCommand($sql_tags);
		$tags_row = $command->queryRow(true, array('id_merchant'=>(int)Yii::app()->user->id));
		
		//4. fetch merchant's open hours
		$sql_hours = 'SELECT week_no, open_hour, close_hour FROM tbl_merchant_open_hours
			WHERE id_merchant=:id_merchant ORDER BY week_no ASC';
		$command = $connection->createCommand($sql_hours);
		$hours = $command->queryAll(true, array('id_merchant'=>Yii::app()->user->id));
		$connection->active = false;
		
		$this->render('index', array(
			'model' => $model,
			'menu_rows' => $menu_rows,
			'special_rows' => $special_rows,
			'week_days' => $week_days,
			'tags' => $tags_row,
			'hours' => $hours,
		));
	}

	public function actionMenu()
	{
		if (isset($_POST['MerchantForm'])) {
			$this->render('menu', array(
				'row_menu' => null,
			));
		} else {
			$connection = Yii::app()->db;
			//1. fetch menu
			$sql_menu = 'SELECT id, url_picture, title, content, week_days,
				UNIX_TIMESTAMP(insert_timestamp) AS uts_insert, UNIX_TIMESTAMP(update_timestamp) AS uts_update
				FROM tbl_merchant_lunch_menu
				WHERE id_merchant=:id_merchant
				ORDER BY insert_timestamp DESC';
			$params_menu = array('id_merchant'=>Yii::app()->user->id);
			$command = $connection->createCommand($sql_menu);
			$rows = $command->queryAll(true, $params_menu);
			$connection->active = false;
			
			$this->render('menu', array(
				'menu_rows' => $rows,
			));
		}
	}
	
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	protected function _loadModel($id)
	{
		$model = MerchantForm::model()->findByPk($id);
		if ($model===null)
			throw new CHttpException(404, 'The requested page does not exist.');
		return $model;
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}